import moment from 'moment';
module.exports = {
	// type date  dateTime dateTimeWeek
	getDateTime(type, connect = "-") {
		var myDate = new Date;
		var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
		var mon = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
		var date = myDate.getDate(); //获取当前日(1-31)
		var week = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
		var hour = myDate.getHours(); //获取当前小时数(0-23)
		var min = myDate.getMinutes(); //获取当前分钟数(0-59)
		var sec = myDate.getSeconds(); //获取当前秒数(0-59)
		var weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];

		var resDate = "";
		var resTime = this.timeConfig(hour) + ':' + this.timeConfig(min) + ":" + this.timeConfig(sec);
		var resWeek = weeks[week]; //星期几

		if (connect == "-") {
			resDate = year + '-' + this.timeConfig(mon) + "-" + this.timeConfig(date);
		} else {
			resDate = year + '年' + this.setTime(mon) + "月" + this.setTime(date) + "日 ";
		}

		if (type == "time") {
			return resTime;
		}

		if (type == "date") {
			return resDate;
		}


		if (type == "dateTime") {
			return resDate + " " + resTime;
		}

		if (type == "dateTimeWeek") {
			return resDate + " " + resTime + " " + resWeek;
		}
	},
	timeConfig(s) {
		return s < 10 ? '0' + s : s;
	},
	timeDiff(startDate, endDate) {
		// 获取相对于现在的时间差
		const timeDiff = moment(startDate).diff(endDate);
		const hourDiff = Math.abs(timeDiff) / 1000 / 60 / 60;

		if (hourDiff < 1) {
			let minuteTime = null;
			if (parseInt(hourDiff * 60) > 0) {
				minuteTime = parseInt(hourDiff * 60) + '分钟前';
			} else {
				minuteTime = '刚刚'
			}

			return minuteTime;

		} else if (hourDiff < 24) {
			return parseInt(hourDiff) + '小时前';
		} else {
			return parseInt(hourDiff / 24) + '天前';
		}
	},
	// 时间戳转为时间 
	strToTime(val, type = "time") {
		var myDate = new Date(val);
		if (type == "time") {
			var hour = myDate.getHours();
			var min = myDate.getMinutes();
			var sec = myDate.getSeconds();

			return this.timeConfig(hour) + ':' + this.timeConfig(min) + ":" + this.timeConfig(sec);
		}
		if (type == "date") {
			var year = myDate.getFullYear();
			var mon = myDate.getMonth() + 1;
			var date = myDate.getDate();

			return year + '-' + this.timeConfig(mon) + "-" + this.timeConfig(date);
		}
	},
	// 当前年第一天
	getFirstDay() {
		var date = new Date();
		return date.getFullYear() + '-01-01';
	},
	// 当前年最后一天
	getLastDay() {
		var date = new Date();
		return date.getFullYear() + '-12-31';
	},

	/********************************************************************************************************/

	// 保留当前页面，跳转到应用内的某个页面，使用uni.navigateBack可以返回到原页面
	goNavPage(url) {
		uni.navigateTo({
			url: url
		})
	},
	// 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面
	goSwtPage(url) {
		uni.switchTab({
			url: url
		})
	},
	// 关闭当前页面，跳转到应用内的某个页面
	goRedrect(url) {
		uni.redirectTo({
			url: url
		})
	},
	// 关闭当前页面，返回上一页面或多级页面
	goBack(delta = 1) {
		uni.navigateBack({
			delta: delta
		});
	},
	// 加载	
	showLoading(title = "数据加载中...") {
		uni.showLoading({
			title: title
		});
	},
	// 取消加载
	hideLoading() {
		uni.hideLoading();
	},
	// 吐司 提示
	showToast(title, time = 2000) {
		uni.showToast({
			title: title,
			mask: false,
			duration: time,
			icon: "none"
		});
	},
	// 同步 会覆盖掉原来该 key 对应的内容
	setData: function(key, value) {
		try {
			uni.setStorageSync(key, value);
		} catch (e) {
			// error
		}
	},
	// 同步 获取指定 key 对应的内容
	getData: function(key) {
		try {
			const value = uni.getStorageSync(key);
			if (value) {
				return value;
			}
		} catch (e) {
			// error
		}

	},
	// 同步移除指定 key
	removeData(key) {
		try {
			uni.removeStorageSync(key);
		} catch (e) {
			// error
		}
	},
	// 同步清理本地数据缓存
	clearData: function() {
		try {
			uni.clearStorageSync();
		} catch (e) {
			// error
		}
	},

	/********************************************************************************************************/


	formatCurrentMonth(date = new Date().toISOString()) {
		var date = new Date(date),
			year = date.getFullYear(),
			month = date.getMonth() + 1,
			day = date.getDate(),
			hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
			minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
			second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
		month >= 1 && month <= 9 ? (month = "0" + month) : "";
		day >= 0 && day <= 9 ? (day = "0" + day) : "";
		// var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
		var timer = year + '-' + month;
		return timer;
	},

	formatTime(time) {
		var newTime = time.split(':')[0] + ":" + time.split(':')[1]
		return newTime
	},

	callMobile(phone) {
		// var Intent = plus.android.importClass("android.content.Intent");
		// var Uri = plus.android.importClass("android.net.Uri");  
		// // 获取主Activity对象的实例  
		// var main = plus.android.runtimeMainActivity();  
		// // 创建Intent  
		// var uri = Uri.parse("tel:" + phone); // 这里可修改电话号码  
		// var call = new Intent("android.intent.action.CALL",uri);  
		// // 调用startActivity方法拨打电话  
		// main.startActivity( call );
		plus.android.requestPermissions(
			["android.permission.CALL_PHONE"],
			function(resultObj) {
				var result = 0;
				for (var i = 0; i < resultObj.granted.length; i++) {
					var grantedPermission = resultObj.granted[i];
					// console.log('已获取的权限：' + grantedPermission);
					result = 1
				}
				for (var i = 0; i < resultObj.deniedPresent.length; i++) {
					var deniedPresentPermission = resultObj.deniedPresent[i];
					// console.log('拒绝本次申请的权限：' + deniedPresentPermission);
					result = 0
				}
				for (var i = 0; i < resultObj.deniedAlways.length; i++) {
					var deniedAlwaysPermission = resultObj.deniedAlways[i];
					// console.log('永久拒绝申请的权限：' + deniedAlwaysPermission);
					result = -1
				}
				// console.log(result);
				if (result == 1) {
					uni.makePhoneCall({
						phoneNumber: phone, //电话号码
						success(ress) {
							// console.log("拨打电话成功",ress)
						},
						fail(err) {
							// console.log("拨打电话失败",'err')
						}
					});
				} else {
					uni.showToast({
						title: "请开启拨号权限",
						icon: "error",
					})
				}
			},
			function(error) {
				// console.log('申请权限错误：' + error.code + " = " + error.message);
			}
		);
	}
};